package com.a918.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.SimpleTimeZone;
import java.util.UUID;

@Component
public class FileUtil {


    private static String uploadpath;


    @Value("${upload_path}")
    public  void setUploadpath(String upload_path) {
        FileUtil.uploadpath = upload_path;
    }

    private static String suffixStrs = "bmp|jpeg|jpg|png|pdf|gif|doc|docx|jfif";

    public static String uploadfile(MultipartFile file, String destDir){

        try{
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
            if (!suffixStrs.contains(suffix)){
                return "fileTypeError";
            }

            String filepath = destDir +File.separator + createNewdir();
            File destfile = new File(FileUtil.uploadpath+filepath);
            if (!destfile.exists()){
                destfile.mkdirs();
            }

            String fileNewName = getFileNewName(destDir)+"."+suffix;
            File f = new File(destfile.getAbsolutePath()+File.separator+fileNewName);
            if(f.exists()&&!(file.getName().contains("constitution"))){
                return "existError";
            }
            file.transferTo(f);
            f.createNewFile();
            return filepath+File.separator+fileNewName;
        }catch (Exception e){
            return "Error";
        }
    }

    private static String createNewdir(){
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        return fmt.format(new Date());
    }

    private static String getFileNewName(String destDir){
        if (destDir.contains("constitution")){
            return "constitution";
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 5);
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmss");
        return fmt.format(new Date())+uuid;
    }



    public static void deleteCPpicture(List<String> picIdList) throws IOException {
        File file = new File("");
        String programepath = file.getCanonicalPath(); //项目路径
        try{
            for (String picid:picIdList){
                String path = programepath+"/src/main/resources/static/images/cangpin/" +picid;
                File myfile = new File(path);
                if (myfile.exists()) {
                    myfile.delete();
                }
            }
        }catch (Exception e){
        }
    }

    public static void deleteRegisterFile(String fileid)throws  IOException{
        File file = new File("");
        String programepath = file.getCanonicalPath(); //项目路径
        try{
            String path = programepath+"/src/main/resources/static/file/registerfile/" +fileid;
            File myfile = new File(path);
            if (myfile.exists()) {
                myfile.delete();
            }
        }catch (Exception e){
        }
    }
}
